<?php namespace Home\Controller;
use Common\Model\GoodsAttr;
use Common\Model\GoodsList;
use Common\Model\TypeAttr;
use Hdphp\Controller\Controller;
use Common\Model\Lnventory;
use Common\Model\Goods;
//详情页面控制器
class ContentController extends Controller{
    public function __init(){}
    //默认页面控制器
    /**
     *
     */
    public function index(){
        $gid=Q('get.gid',0,'intval');
        $lnventory=new Lnventory();$goods=new Goods();
        $goodsAttr=new GoodsAttr();
        //获取当前商品详情数据
        $lData=$lnventory->where("goods_gid={$gid}")->find();
        $arr=explode('|',$lData['picture']);
        $lData['picture']=$arr;
//        P($lData['picture']);
        //获得当前商品数据
        $goodsData=$goods->where("gid={$gid}")->find();
        //通过当前商品属性表关联类型属性表获取商品属性数据
        $gaData=$goodsAttr
               ->join("typeattr",'type_attr_taid','=','taid')
               ->where("goods_gid={$gid} AND attr_type=1")
               ->groupBy('taid')
               ->get();
//        p( $gaData);exit;
        foreach($gaData as $k=>$v){
            $gaData[$k]['value']=$goodsAttr->where("type_attr_taid={$v['taid']} AND goods_gid={$gid}")->lists('gaid,gavalue');
        }

        View::with('gaData',$gaData);
        View::with('goodsData',$goodsData);
        View::with('lData',$lData);
        View::make();
    }
    //异步查询数据
    public function select(){
        $gid=Q('get.gid',0,'intval');
        $str=rtrim(Q('post.c'),',');
        $goodsList=new GoodsList();$goods=new Goods();$goodsAttr=new GoodsAttr();
        //获取本条商品列表数据
        $reData=$goodsList->where("groupid='{$str}' AND goods_gid={$gid}")->find();
        //获取商品价格
        $price=$goods->where("gid={$gid}")->pluck('price');
        //获取商品各个属性的附加价格
        $added=$goodsAttr->where("gaid IN({$str})")->lists('extra_price');
        foreach ($added as $v){
            $pri='';
            $pri +=$v;
        }
        $price=intval($pri)+$price;
        $reData['added']=sprintf('%.2f',$price);
        $this->ajax($reData);
    }
}